﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormCancelSaleManager : Form
    {
        public FormCancelSaleManager()
        {
            InitializeComponent();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            //string sql = "SELECT order_id,customer_id,customer_name,user_id,user_name,good_id,good_name,good_num,good_price,receiver_name,total_prices,money_receive,discount,receive_company,strftime('%Y-%m-%d',add_date) as add_date,remark FROM ff_order where 1=1  ";
            string sql = "SELECT cancel_order_id,order_id,customer_id,customer_name,good_id,good_name,good_num,good_price,receiver_name,total_prices,discount,strftime('%Y-%m-%d',add_date) as add_date,remark FROM ff_cancle_order where 1=1  ";
            if (this.textBoxCancelid.Text.Trim() != "")
            {
                sql += " and cancel_order_id like '%" + this.textBoxCancelid.Text.Trim() + "%'";
            }
            if (this.textBoxGoodname.Text.Trim() != "")
            {
                sql += " and good_name like '%" + this.textBoxGoodname.Text.Trim() + "%'";
            }
            if (this.textBoxOrderID.Text.Trim() != "")
            {
                sql += " and order_id like '%" + this.textBoxOrderID.Text.Trim() + "%'";
            }
            if (this.textBoxCustomername.Text.Trim() != "")
            {
                sql += " and customer_name like '%" + this.textBoxCustomername.Text.Trim() + "%'";
            }
            if (this.checkBoxStart.Checked)
            {
                sql += " and datetime(add_date) >=datetime('" + dateTimePickerStartday.Value.ToString("yyyy-MM-dd") + "')";
            }
            if (this.checkBoxEnd.Checked)
            {
                sql += " and datetime(add_date) <datetime('" + dateTimePickerEndday.Value.AddDays(1).ToString("yyyy-MM-dd") + "')";
            }

            try
            {
                DataSet ds = SQLiteHelper.Query(sql);
                dataGridView1.DataSource = ds.Tables[0];
                if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
                {
                    MessageBox.Show("没有相关记录！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //dataGridView1.DataSource = null;
                    return;
                }

                return;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string selectedId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            CancelOrder order = CancelOrderMethod.getEntityById(selectedId);

            if (order != null)
            {
                FormCancleSale frm = new FormCancleSale( selectedId);
                frm.Owner = this;
                frm.ShowDialog();
                buttonQuery_Click(sender, e);
            }
        }
    }
}
